package com.xuan.temper.controller;

import com.xuan.temper.entity.*;
import com.xuan.temper.service.CodeTempService;
import com.xuan.temper.service.ExponentialInfoService;
import com.xuan.temper.service.ExponentialTemperatureService;
import com.xuan.temper.utils.CalculateExponentialTemperatureUtils;
import com.xuan.temper.utils.dateUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人： king xuan
 * @创建时间：2020/9/6
 * @描述： 查询指定日期并计算 当天温度
 */
@RestController
@RequestMapping("/specifiedcalculation")
public class SpecifiedCalculation {

    //获取当天日期十年前的所有pe，pb加权
    @Autowired
    private ExponentialTemperatureService exponentialTemperatureService;

    @Autowired
    private CodeTempService codeTempService;

    @Autowired
    private ExponentialInfoService exponentialInfoService;

    private static Logger log = LoggerFactory.getLogger(SpecifiedCalculation.class);

    @RequestMapping("/temper")
    public ResponeBean codeTempController(@RequestParam("endTime") String endTime,
                                          @RequestParam("name") String[] name) throws ParseException {
        log.info("输入参数", endTime, name);
        String current[]={"pe_ttm.mcw","pe_ttm.ew","pe_ttm.ewpvo","pe_ttm.avg","pe_ttm.median","pb.mcw","pb.ew","pb.ewpvo","pb.avg","pb.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String ten [] ={"pe_ttm.y10.mcw","pe_ttm.y10.ew","pe_ttm.y10.ewpvo","pe_ttm.y10.avg","pe_ttm.y10.median","pb.y10.mcw","pb.y10.ew","pb.y10.ewpvo","pb.y10.avg","pb.y10.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String[]  combination=null;
        combination = (String[]) ArrayUtils.addAll(current, ten);
        ArrayList<ExponentialTemperature> exponentialTemperatureArrayList = new ArrayList<>();
        Object data = null;
        for (String entry : name) {
            //根据传入名称查询信息 ，看传入名称是否正确
            ExponentialInfo exponentialInfo = exponentialInfoService.queryName(entry);
            if (exponentialInfo==null) {
                data= "传入名称有误" + entry;
            }
            else {
                //根据指定日期算出10年前时间
                String startTime = dateUtils.getTenYears(endTime);
                //根据指定时间查询出当前基金pe，pb加权
                List<ExponentialTemperature> exponentialTemperatures = exponentialTemperatureService.specifiedCalculation(entry, startTime, endTime);
                //查出有多少条数
                int size = exponentialTemperatures.size();
                if (size > 2300) {
                    //根据加权计算出指数温度，只计算出传入时间的温度
                    ExponentialTemperature exponentialTheTemperature = CalculateExponentialTemperatureUtils.CalculateExponentialTheTemperature(exponentialTemperatures,endTime);
                    exponentialTemperatureArrayList.add(exponentialTheTemperature);
                    log.info("计算成功", new ExponentialTemperature[]{exponentialTheTemperature});
                    if (exponentialTheTemperature.getName()==null){
                        data="当前传入日期为非交易日，传入日期必须为交易日";
                    }else {
                        data = exponentialTemperatureArrayList;
                    }
                } else {
                    List<Data> returnData = codeTempService.codeTemp(startTime, endTime, exponentialInfo.getStockCode(), current);
                    ArrayList<Object> list = new ArrayList<>();
                    ExponentialTemperature exponentialTemperature1 = new ExponentialTemperature();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    for (Data returnDatum : returnData) {
                        String returnEndeTime = format.format(returnDatum.getDate());
                        if (returnEndeTime.equals(endTime)) {
                            System.out.println(returnEndeTime);
                            System.out.println(endTime);
                            System.out.println("-----------------------------");
                            exponentialTemperature1.setTime(returnDatum.getDate());
                            exponentialTemperature1.setPeJq(returnDatum.getPe_ttm().getMcw());
                            exponentialTemperature1.setPeTemp(returnDatum.getPe_ttm().getPeTemper());
                            exponentialTemperature1.setPbJq(returnDatum.getPb().getMcw());
                            exponentialTemperature1.setPbTemp(returnDatum.getPb().getPbTemper());
                            exponentialTemperature1.setCurrenTemp(returnDatum.getCurrenTemp());
                            exponentialTemperature1.setStockCode(returnDatum.getStockCode());
                            exponentialTemperature1.setName(returnDatum.getStockName());
                            list.add(exponentialTemperature1);
                            break;
                        }
                    }
                  if (exponentialTemperature1.getName()==null){
                      data="当前传入日期为非交易日，传入日期必须为交易日";
                  }else {
                      data = list;
                  }

                }
            }
        }
        return new ResponeBean("200", "success", data);
    }
}
